package cn.tedu.jsd2210.csmall.product.service.impl;

import cn.tedu.jsd2210.csmall.product.ex.ServiceException;
import cn.tedu.jsd2210.csmall.product.mapper.BrandMapper;
import cn.tedu.jsd2210.csmall.product.pojo.dto.BrandAddNewDto;
import cn.tedu.jsd2210.csmall.product.pojo.dto.BrandUpdateDto;
import cn.tedu.jsd2210.csmall.product.pojo.entity.Brand;
import cn.tedu.jsd2210.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.jsd2210.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.jsd2210.csmall.product.service.IBrandService;
import cn.tedu.jsd2210.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service

public class BrandServiceImpl implements IBrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public void addNew(BrandAddNewDto BrandAddNewDto) {

        String name=BrandAddNewDto.getName();
        int count = brandMapper.countByName(name);
        log.debug("根据名称[{}]统计数量,结果:{}",name,count);
        if(count>0){
            String message="添加品牌失败,品牌名称已被占用!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }

        //创建实例对象
        Brand brand=new Brand();
        //复制属性
        BeanUtils.copyProperties(BrandAddNewDto,brand);

        brand.setSales(99);
        brand.setProductCount(99);
        brand.setCommentCount(99);
        brand.setPositiveCommentCount(99);

        //调用mapper插入数据
        int rows = brandMapper.insert(brand);
        if(rows!=1){
            String message="添加品牌失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }


    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理[根据ID:{}删除品牌]的业务",id);
        BrandStandardVO queryResult = brandMapper.getStandardById(id);
        if(queryResult==null){
            String message="删除品牌失败,尝试删除的品牌数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        log.debug("开始执行[根据ID:{}删除品牌]",id);
        int rows = brandMapper.deleteById(id);
        if(rows!=1){
            String message="删除品牌失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE,message);
        }
    }

    @Override
    public void updateInfoById(BrandUpdateDto brandUpdateDto, Long id) {
        log.debug("开始处理[根据ID:{}修改品牌]的业务",id);
        BrandStandardVO queryResult = brandMapper.getStandardById(id);
        if(queryResult==null){
            String message="修改品牌失败,尝试修改的品牌数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        int count = brandMapper.countByNameAndNotId(id, brandUpdateDto.getName());
        if(count>0){
            String message="添加品牌失败,品牌名称已被占用!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }

        log.debug("开始执行[根据ID:{}修改品牌]",id);
        Brand brand=new Brand();
        BeanUtils.copyProperties(brandUpdateDto,brand);
        brand.setId(id);
        int rows = brandMapper.update(brand);
        if(rows<1){
            String message="修改品牌失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }


    @Override
    public void setEnable(Long id) {
        log.debug("开始处理[根据ID:{}设置品牌可用]的业务",id);
        updateEnableById(id,1);
    }

    @Override
    public void setDisable(Long id) {
        log.debug("开始处理[根据ID:{}设置品牌禁用]的业务",id);
        updateEnableById(id,0);
    }

    @Override
    public BrandStandardVO getStandardById(Long id) {
        log.debug("开始处理[根据ID:{}查询品牌详情]的业务",id);
        BrandStandardVO queryResult = brandMapper.getStandardById(id);
        if(queryResult==null){
            String message="查询类别失败,尝试查询的品牌数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return queryResult;
    }

    @Override
    public List<BrandListItemVO> list() {
        log.debug("开始处理查询品牌列表的业务,无参数");
        List<BrandListItemVO> list = brandMapper.list();
        return list;
    }

    private void updateEnableById(Long id, Integer enable) {
        //调用mapper对象的getStandardById方法查询
        BrandStandardVO currentBrand = brandMapper.getStandardById(id);
        //判断结果是否为null,是则抛出异常
        if(currentBrand==null){
            String message="修改品牌失败,尝试修改的品牌数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        //判断查询结果中的enable与参数enable是否相同,是则抛出异常(当前状态与目标状态相同,没必要修改)
        Integer currentEnable = currentBrand.getEnable();
        if(currentEnable==enable){
            String message=ENABLE_TEXT[enable]+"失败,此品牌已经处于该状态!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        //创建Brand对象
        Brand updateBrand=new Brand();
        //向对象封装属性
        updateBrand.setId(id);
        updateBrand.setEnable(enable);
        //调用mapper方法进行更新
        int rows = brandMapper.update(updateBrand);
        if(rows!=1){
            String message="修改品牌失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }


}
